import {MessageModel} from "../../models/message.js";
var _MessageModel = new MessageModel();
Component({
  properties: {
    showData:{
      type:Object
    }
  },
  data: {
    windowWidth:750,
    px2rpx:2,
    padding:30,
    margin:10,
    pictures: [
      'https://p0.meituan.net/movie/ea4ac75173a8273f3956e514a4c78018253143.jpeg',
      'https://p0.meituan.net/movie/5d4fa35c6d1215b5689257307c461dd2541448.jpeg',
      'https://p0.meituan.net/movie/0c49f98a93881b65b58c349eed219dba290900.jpg',
      'https://p1.meituan.net/movie/45f98822bd15082ae3932b6108b17a01265779.jpg',
  
    ],
    imageSize:{},
  },
  attached: function (e) {
    const sysInfo = wx.getSystemInfoSync();
    this.setData({
      windowWidth: sysInfo.windowWidth,
      px2rpx: 750 /sysInfo.windowWidth
    })
    this.calcImageSize();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onLike:function(option){
      let params = {
        "messageId":option.target.dataset.messageid,
        "userId": option.target.dataset.userid
      }
      if (option.detail.behivor=='like'){
        _MessageModel.prise(params,function(data){

        })
      }else{
        _MessageModel.unprise(params, function (data) {

        })
      }
    },
    calcImageSize:function(e){
    
      let imageSize;
      //每一行图片的宽度
      let lineWidth = this.data.windowWidth*this.data.px2rpx - this.data.padding*2 - this.data.margin*3;
      let everyImageWidth
      if(this.data.pictures.length==1){
        everyImageWidth = Math.floor(lineWidth-150);
        imageSize = {
          width: everyImageWidth
        }
      }else if (this.data.pictures.length == 1){
        everyImageWidth = Math.floor(lineWidth / 3);
       
        imageSize = {
          width: everyImageWidth+20,
          height: everyImageWidth+20
        }
      }else{
        everyImageWidth= Math.floor(lineWidth/3);
        imageSize = {
          width: everyImageWidth,
          height: everyImageWidth
        }
      }
 
      
      this.setData({
        imageSize: imageSize
      })
    },
    imageLoad: function (e) {
    
      if(this.data.pictures.length==1){
        //单位rpx
        var originWidth = e.detail.width * this.data.px2rpx,
          originHeight = e.detail.height * this.data.px2rpx,
          ratio = originWidth / originHeight;
        var viewWidth = 710, viewHeight//设定一个初始宽度
        //当它的宽度大于初始宽度时，实际效果跟mode=widthFix一致
        //console.log("originWidth"+originWidth)
        if (originWidth >= viewWidth) {
          //宽度等于viewWidth,只需要求出高度就能实现自适应
          viewHeight = viewWidth / ratio;
        } else {
          //如果宽度小于初始值，这时就不要缩放了
          viewWidth = originWidth;
          viewHeight = originHeight;
        }
        var imageSize = this.data.imageSize;
        imageSize= {
          width: viewWidth,
          height: viewHeight
        }
        this.setData({
          imageSize: imageSize
        })
      }
    },
    previewImage: function (e) {
      let index = e.currentTarget.dataset.index;
      let pictures = this.data.pictures;
      wx.previewImage({
        current: pictures[index],
        urls: pictures
      })
    }
  }
})
